//
//  ITHomePublicTableViewCell.m
//  ITHome
//
//  Created by lanou3g on 15/7/16.
//  Copyright (c) 2015年 YAX. All rights reserved.
//

#import "ITHomePublicTableViewCell.h"
#import "ITHomePublicModel.h"
#import "Reachability.h"

@implementation ITHomePublicTableViewCell

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        self.picture = [[UIImageView alloc]initWithFrame:CGRectMake(5, 5, kScreenIp6(90), kScreenIp6(80))];
        self.picture.image = [UIImage imageNamed:@"content_cell_bg"]; // 假图 it家园
        [self.contentView addSubview:_picture];
        
        self.titleLabel = [[UILabel alloc]initWithFrame:CGRectMake(self.picture.frame.origin.x + kScreenIp6(90) + 5, 5, kScreenWidth - 15 - kScreenIp6(90), kScreenIp6(60))];
        self.titleLabel.font = [UIFont systemFontOfSize:14];
        self.titleLabel.numberOfLines = 0;
        [self.contentView addSubview:_titleLabel];
        
        self.time = [[UILabel alloc]initWithFrame:CGRectMake(self.picture.frame.origin.x + kScreenIp6(90) + 5, self.picture.origin.y + kScreenIp6(80) - 20, kScreenIp6(80), 20)];
        self.time.textColor = [UIColor RGBColor:145 green:145 blue:145 alpha:1];
        self.time.font = [UIFont systemFontOfSize:11];
        self.time.textAlignment = NSTextAlignmentCenter;
        
        [self.contentView addSubview:self.time];
        
        self.reply = [[UIImageView alloc]initWithFrame:CGRectMake(kScreenWidth - 45, self.picture.origin.y + kScreenIp6(80) - 20, 20, 20)];
        self.reply.image = [UIImage imageNamed:@"news_content_comment_n"];
        [self.contentView addSubview:self.reply];
        
        self.replyCount = [[UILabel alloc]initWithFrame:CGRectMake(self.reply.origin.x + 20, self.time.origin.y, 20, 20)];
        self.replyCount.textAlignment = NSTextAlignmentCenter;
        self.replyCount.textColor = [UIColor RGBColor:145 green:145 blue:145 alpha:1];
        self.replyCount.font = [UIFont systemFontOfSize:11];
        [self.contentView addSubview:self.replyCount];
        
    }
    return self;
}


#pragma mark - cell赋值
- (void)loadCellWithDataModel:(ITHomePublicModel *)itHomePublicModel
{
//    Reachability *r = [Reachability reachabilityWithHostName:@"www.baidu.com"];
//    // 如果没网
//    if ([r currentReachabilityStatus] == NotReachable)
//    {

        [self.picture sd_setImageWithURL:[NSURL URLWithString:itHomePublicModel.feature_img] placeholderImage:[UIImage imageNamed:@"content_cell_bg"]];
    

//    }
//    // 如果3g网
//    else if ([r currentReachabilityStatus] == ReachableViaWWAN)
//    {
//        if ([[NSUserDefaults standardUserDefaults] boolForKey:@"isPicture"] == YES)
//        {
//           
//        }
//        else
//        {
//           [self.picture sd_setImageWithURL:[NSURL URLWithString:itHomePublicModel.feature_img] placeholderImage:[UIImage imageNamed:@"content_cell_bg"]];
//        }
//    }
//    // wifi
//    else if ([r currentReachabilityStatus] == ReachableViaWiFi)
//    {
//
//        [self.picture sd_setImageWithURL:[NSURL URLWithString:itHomePublicModel.feature_img] placeholderImage:[UIImage imageNamed:@"content_cell_bg"]];
//    }

    // 计算正文Label的高度
    CGRect bounds = [itHomePublicModel.title boundingRectWithSize:CGSizeMake(kScreenWidth - 15 - kScreenIp6(90), MAXFLOAT) options:NSStringDrawingUsesLineFragmentOrigin attributes:[NSDictionary dictionaryWithObject:[UIFont systemFontOfSize:14] forKey:NSFontAttributeName] context:nil];
    if (bounds.size.height + 10 <= self.time.origin.y) {
        self.titleLabel.frame = CGRectMake(self.picture.frame.origin.x + kScreenIp6(90) + 5, 5, kScreenWidth - 15 - kScreenIp6(90), bounds.size.height);
        self.titleLabel.text = itHomePublicModel.title;
    }else{
        self.titleLabel.frame = CGRectMake(self.picture.frame.origin.x + kScreenIp6(90) + 5, 5, kScreenWidth - 15 - kScreenIp6(90), self.time.origin.y - 5);
        self.titleLabel.text = itHomePublicModel.title;
    }
    
    // 判断如果评论数等于0  隐藏图标
    if (itHomePublicModel.replies_count.integerValue == 0)
    {
        self.reply.hidden = YES;
        self.replyCount.hidden = YES;
    }
    else
    {
//         评论数
        self.replyCount.text = [NSString stringWithFormat:@"%@",itHomePublicModel.replies_count]; // 评论数
        self.reply.hidden = NO;
        self.replyCount.hidden = NO;
    }

    // 时间
    NSString *string = [self getNewDate:itHomePublicModel.created_at];
    self.time.text = [self getDate:string];
}


#pragma 移动互联页面赋值
- (void)setYiDongModel:(RecentActivityModel *)model
{
    // 图片赋值
   [self.picture sd_setImageWithURL:[NSURL URLWithString:model.imgsrc] placeholderImage:[UIImage imageNamed:@"content_cell_bg"]];
    // 计算正文Label的高度
    CGRect bounds = [model.title boundingRectWithSize:CGSizeMake(kScreenWidth - 15 - kScreenIp6(90), MAXFLOAT) options:NSStringDrawingUsesLineFragmentOrigin attributes:[NSDictionary dictionaryWithObject:[UIFont systemFontOfSize:14] forKey:NSFontAttributeName] context:nil];
    // 重新赋值label
    if (bounds.size.height + 10 <= self.time.origin.y) {
        self.titleLabel.frame = CGRectMake(self.picture.frame.origin.x + kScreenIp6(90) + 5, 5, kScreenWidth - 15 - kScreenIp6(90), bounds.size.height);
        self.titleLabel.text = model.title;
    }else{
        self.titleLabel.frame = CGRectMake(self.picture.frame.origin.x + kScreenIp6(90) + 5, 5, kScreenWidth - 15 - kScreenIp6(90), self.time.origin.y - 5);
        self.titleLabel.text = model.title;
    }
    // 赋值时间
    NSString *string = [self getNewDate:model.lmodify];
    self.time.text = [self getDate:string];
    
    // 评论人数
    if (model.replyCount !=0)
    {
        self.replyCount.text = [NSString stringWithFormat:@"%d",model.replyCount];
        self.replyCount.hidden = NO;
        self.reply.hidden = NO;
    }
    else
    {
        self.replyCount.hidden = YES;
        self.reply.hidden = YES;
    }
    self.titleLabel.frame = CGRectMake(self.picture.frame.origin.x + kScreenIp6(90) + 5, 5, kScreenWidth - 15 - kScreenIp6(90), bounds.size.height);
}

#pragma mark -  收藏页面
- (void)setCollectionedModel:(FMDBManageModel *)model
{

    self.picture.image = model.shareImage;
//    NSLog(@"----%@----",model.shareImage);

    [self.picture sd_setImageWithURL:[NSURL URLWithString:model.feature_img] placeholderImage:[UIImage imageNamed:@"content_cell_bg"]];
    
    // 计算正文Label的高度
    CGRect bounds = [model.shareTitle boundingRectWithSize:CGSizeMake(kScreenWidth - 15 - kScreenIp6(90), MAXFLOAT) options:NSStringDrawingUsesLineFragmentOrigin attributes:[NSDictionary dictionaryWithObject:[UIFont systemFontOfSize:14] forKey:NSFontAttributeName] context:nil];
    // 重新赋值label
    if (bounds.size.height + 10 <= self.time.origin.y) {
        self.titleLabel.frame = CGRectMake(self.picture.frame.origin.x + kScreenIp6(90) + 5, 5, kScreenWidth - 15 - kScreenIp6(90), bounds.size.height);
        self.titleLabel.text = model.shareTitle;
    }else{
        self.titleLabel.frame = CGRectMake(self.picture.frame.origin.x + kScreenIp6(90) + 5, 5, kScreenWidth - 15 - kScreenIp6(90), self.time.origin.y - 5);
        self.titleLabel.text = model.shareTitle;
    }
    // 时间
    self.time.text = model.created_at;
    // 评论人数
    if (model.commentsCount != nil)
    {
        self.replyCount.text = model.commentsCount;
        self.replyCount.hidden = NO;
        self.reply.hidden = NO;
    }
    else
    {
        self.replyCount.hidden = YES;
        self.reply.hidden = YES;
    }
    
}

// 根据解析的时间得到距离当前时间的间隔
- (NSString *)getDate:(NSString *)newDate
{
    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc]init];
    [dateFormatter setDateFormat:@"yyyy-MM-dd HH:mm:ss"];
    NSDate *date = [dateFormatter dateFromString:newDate];
    NSTimeInterval timeInterval = [date timeIntervalSinceNow];
    timeInterval = -timeInterval;
    long temp = 0;
    NSString *result;
    if (timeInterval < 60) {
        result = [NSString stringWithFormat:@"刚刚"];
    }
    else if((temp = timeInterval/60) <60){
        result = [NSString stringWithFormat:@"%ld分前",temp];
    }
    
    else if((temp = temp/60) <24){
        result = [NSString stringWithFormat:@"%ld小前",temp];
    }
    
    else if((temp = temp/24) <30){
        result = [NSString stringWithFormat:@"%ld天前",temp];
    }
    
    else if((temp = temp/30) <12){
        result = [NSString stringWithFormat:@"%ld月前",temp];
    }
    else{
        temp = temp/12;
        result = [NSString stringWithFormat:@"%ld年前",temp];
    }
    
    return  result;
    
}

// 截取自己想要的时间字符串格式
- (NSString *)getNewDate:(NSString *)time
{
    NSString *string1 = [time substringWithRange:NSMakeRange(0, 10)];
    NSString *string2 = [time substringWithRange:NSMakeRange(11, 8)];
    NSString *newString = [NSString stringWithFormat:@"%@ %@",string1,string2];
    return newString;
}


@end
